package com.itheima.service.impl;

import com.itheima.dto.MenuDTO;
import com.itheima.entity.SysMenu;
import com.itheima.mapper.SysMenuMapper;
import com.itheima.service.SysMenuService;
import com.itheima.vo.MenuVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuMapper sysMenuMapper;
    /**
     * 所有菜单
     * @return
     */
    @Override
    public List<MenuVO> allData() {
        List<SysMenu> sysMenuList1 = sysMenuMapper.selectByPId(0);
        List<MenuVO> menuVOList2 = sysMenuList1.stream().map(new Function<SysMenu, MenuVO>() {
            @Override
            public MenuVO apply(SysMenu sysMenu) {
                MenuVO menuVO = sysMenu.transformMenuVO();
                return menuVO;
            }
        }).collect(Collectors.toList());
        menuVOList2.forEach(menuVO2 -> {
            List<SysMenu> sysMenuList2 = sysMenuMapper.selectByPId(menuVO2.getId());
            List<MenuVO> menuVOList3 = sysMenuList2.stream().map(new Function<SysMenu, MenuVO>() {
                @Override
                public MenuVO apply(SysMenu sysMenu) {
                    MenuVO menuVO = sysMenu.transformMenuVO();
                    return menuVO;
                }
            }).collect(Collectors.toList());
            menuVOList3.forEach(menuVO3 ->{
                List<SysMenu> sysMenuList3 = sysMenuMapper.selectByPId(menuVO3.getId());
                List<MenuVO> menuVOList4 = sysMenuList3.stream().map(new Function<SysMenu, MenuVO>() {
                    @Override
                    public MenuVO apply(SysMenu sysMenu) {
                        MenuVO menuVO = sysMenu.transformMenuVO();
                        return menuVO;
                    }
                }).collect(Collectors.toList());
                menuVO3.setChildren(menuVOList4);
            } );
            menuVO2.setChildren(menuVOList3);
        });
        return menuVOList2;
    }

    /**
     * 菜单添加
     * @param menuDTO
     */
    @Override
    public void save(MenuDTO menuDTO) {
        SysMenu sysMenu = menuDTO.transformSysMenu();
        sysMenu.setDelFlag(0);
        sysMenu.setDefaultData("0");
        sysMenuMapper.insert(sysMenu);
    }

    /**
     * 删除菜单
     * @param id
     */
    @Override
    public void delete(Integer id) {
        sysMenuMapper.deleteById(id);
    }

    /**
     * 查询回显
     * @param id
     * @return
     */
    @Override
    public MenuVO rowInfo(Integer id) {
        SysMenu sysMenu = sysMenuMapper.selectById(id);
        MenuVO menuVO = sysMenu.transformMenuVO();
        return menuVO;
    }

    /**
     * 修改
     * @param menuDTO
     */
    @Override
    public void update(MenuDTO menuDTO) {
//        SysMenu sysMenu = sysMenuMapper.selectById(menuDTO.getId());
        SysMenu sysMenu = menuDTO.transformSysMenu();
        sysMenuMapper.update(sysMenu);
    }


}
